<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Extrasoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('extrasoption');
	}
	public function ajax_extrasoption_get(){
		$extrasoptionEntity =  new CI_Extras_option_entity();
		$extrasoption = $extrasoptionEntity->getExtrasOptionByCode($_REQUEST['code']);
		echo json_encode($extrasoption);
	}
	public function ajax_extrasoption_list(){
		$extrasoptionEntity =  new CI_Extras_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$extrasoptionList = $extrasoptionEntity->getExtrasOptionListByLanguage($defaultLanguageId);
		$data['extrasoptionList'] = $extrasoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/extrasoptions/index', $data);
	}
	public function ajax_extrasoption_save(){
		$extrasoptionNameList = $_REQUEST['extrasoption'];
		$extrasoptionNameDefault = '';
		foreach ($extrasoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$extrasoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($extrasoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_extrasoption_require');
		}else{
			$code = 0;
			foreach ($extrasoptionNameList as $key => $value) {
				$extrasoptionEntity =  new CI_Extras_option_entity();
				if($value != null && $value != ''){
					$extrasoptionEntity->setExtras($value);
				}else{
					$extrasoptionEntity->setExtras($extrasoptionNameDefault);
				}
				$extrasoptionEntity->setLanguageId($key);
				$extrasoptionEntity->setCode($code);
				$id = $extrasoptionEntity->save();
				if($code == 0){
					$code = $id;
					$extrasoptionEntity->setId($id);
					$extrasoptionEntity->setCode($code);
					$extrasoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_extrasoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_extrasoption_update(){
		$extrasoptionNameList = $_REQUEST['extrasoption'];
		$extrasoptionIdList = $_REQUEST['extrasoption_id'];
		$extrasoptionCode = $_REQUEST['extrasoption_code'];
		$extrasoptionNameDefault = '';
		foreach ($extrasoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$extrasoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($extrasoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_extrasoption_require');
		}else{
			foreach ($extrasoptionNameList as $key => $value) {
				$extrasoptionEntity =  new CI_Extras_option_entity();
				if($value != null && $value != ''){
					$extrasoptionEntity->setExtras($value);
				}else{
					$extrasoptionEntity->setExtras($extrasoptionNameDefault);
				}
				$extrasoptionEntity->setLanguageId($key);
				$extrasoptionEntity->setCode($extrasoptionCode);
				$extrasoptionEntity->setId($extrasoptionIdList[$key]);
				$extrasoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_extrasoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_extrasoption_delete(){
		$result = array();
		$extrasoptionEntity =  new CI_Extras_option_entity();
		$result['result'] = $extrasoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_extrasoption_delete_success');
		}else{
			$result['message'] = lang('escort_extrasoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>